﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;

/// <Summary> 
/// Finds sessions hosted by other players on Lan
/// </Summary> 

namespace Imagine
{
    class FindSession : GameScreen
    {
       string tag;
       AvailableNetworkSessionCollection availSession;
       public override bool Init(object extraData)
       {
           tag = "Not Found yet";
           Refresh();
           return true; 
       }
       public override bool Draw(GameTime gameTime, SpriteBatch spriteBatch)
       {
            Engine.Instance().GraphicsDevice.Clear(Color.Brown);
            spriteBatch.DrawString(Engine.Instance().SpriteFont, "FindSessionScreen", new Vector2(200,1),Color.Chocolate);
            spriteBatch.DrawString(Engine.Instance().SpriteFont, "Sessions", new Vector2(400, 200), Color.Chocolate);
            spriteBatch.DrawString(Engine.Instance().SpriteFont, "MaxPlayers", new Vector2(600, 200), Color.Chocolate);
            spriteBatch.DrawString(Engine.Instance().SpriteFont, "LevelTime", new Vector2(800, 200), Color.Chocolate);
            //Display Info for any session if found
            if(availSession.Count > 0)
            {
                foreach (AvailableNetworkSession session in availSession)
                {
                    Engine.Instance().SpriteBatch.DrawString(Engine.Instance().SpriteFont, session.SessionProperties[1].ToString(), new Vector2(630, 230), Color.Chocolate);
                    Engine.Instance().SpriteBatch.DrawString(Engine.Instance().SpriteFont, session.SessionProperties[2].ToString(), new Vector2(830, 230), Color.Chocolate);
                    Engine.Instance().SpriteBatch.DrawString(Engine.Instance().SpriteFont, session.HostGamertag, new Vector2(430,230),Color.Chocolate);
                }
            }
           
           return true; 
       }

       public override bool Update(GameTime gameTime)
       {
           if (InputManager.Instance().IsKeyDown(Keys.Space))
               Refresh();

           if (InputManager.Instance().IsKeyDown(Keys.Enter))
           {
               if (availSession.Count != 0)
               {
                   NetworkManager.Instance().Session = NetworkSession.Join(availSession[0]);
                   NetworkManager.Instance().HookEventHandlers(NetworkManager.Instance().Session);

                   ScreenManager.Instance().SetScreen(ImagineScreens.eLobbyScreen, ScreenState.Dead, null);
               }
           }
           return true;
       }

       public override bool Release()
       {
           return true; 
       }

        //Searches for Sessions, when refreshed
        private void Refresh()
        {
            //Only 1 player per PC             
            //Might have to put into Try{}Catch{} block
            availSession = NetworkSession.Find(NetworkSessionType.SystemLink, 1/*1-4*/, null);
        }

    
    }
}

